package org.yzz.auth.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.yzz.auth.dto.LoginDTO;
import org.yzz.auth.service.ILoginService;
import org.yzz.base.utils.LoginContext;
import org.yzz.org.domain.Employee;
import org.yzz.org.mapper.EmployeeMapper;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private EmployeeMapper mapper;
    @Override
    public Map<String, Object> login(LoginDTO employee) {
        if(StringUtils.isEmpty(employee.getUsername())||StringUtils.isEmpty(employee.getPassword()))
            throw new RuntimeException("账号或密码错误，请重新输入");

        Employee employee1 = mapper.selectByName(employee.getUsername());
        if(Objects.isNull(employee1))
            throw new RuntimeException("账号或密码错误，请重新输入");
        if(!employee1.getPassword().equals(employee.getPassword()))
            throw new RuntimeException("账号或密码错误，请重新输入");
        String uuid = UUID.randomUUID().toString();
        LoginContext.map.put(uuid,employee1);
        Map<String, Object> map = new HashMap<>();
        map.put("token",uuid);
        employee1.setPassword(null);
        map.put("user",employee1);
        return map;
    }
}
